package com.d4a5.mapping.google2
{
	import com.d4a5.mapping.core.AmfFactory;
	import com.d4a5.mapping.core.AmfObject;
	import com.d4a5.mapping.core.AmfPackage;
	import com.d4a5.mapping.core.AmfPackages;
	
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;

	public class wmgOverlayLayerShpPuller extends wmgOverlayLayerAmfPuller
	{
		protected var _shpUrl:String;
		protected var _shpUrlChanged:Boolean = false;
		protected var _shpPuller:URLLoader;
		protected var _dbfPuller:URLLoader;
		protected var _shpBytes:ByteArray;
		protected var _dbfBytes:ByteArray;
		public var hasDbf:Boolean = false;
		
		public function wmgOverlayLayerShpPuller()
		{
			super();
			
			this._shpPuller = new URLLoader();
			this._shpPuller.addEventListener(Event.COMPLETE,onShpPullComplete);
			this._shpPuller.dataFormat = URLLoaderDataFormat.BINARY;
			
			this._dbfPuller = new URLLoader();
			this._dbfPuller.addEventListener(Event.COMPLETE,onDbfPullComplete);
			this._dbfPuller.dataFormat = URLLoaderDataFormat.BINARY;
		}
		
		public function get shpUrl() : String {
			return this._shpUrl;
		}
		
		public function set shpUrl( u:String ) : void {
			if( u != null && u != "" && this._shpUrl != u ){
				this._shpUrl = u;
				this._shpUrlChanged = true;
				this.invalidateProperties();
			}
		}
		
		override protected function commitProperties():void {
			if( this.pane && this.pane.map ){
				if( this._shpUrlChanged ){
					this._shpUrlChanged = false
					
					this.removeAll();
					this._dbfBytes = this._shpBytes = null;	
					
					this._shpPuller.load( new URLRequest( this._shpUrl ) );
					if( this.hasDbf ){
						this._dbfPuller.load( new URLRequest( this._shpUrl.replace(".shp",".dbf") ) );
					}
				}
			}
			else{
				this.invalidateProperties();
			}
			
			super.commitProperties();
		}
		
		protected function onShpPullComplete( event:Event ) : void {
			this._shpBytes = event.target.data as ByteArray;
			this.doneGettingData();
		}
		
		protected function onDbfPullComplete( event:Event ) : void {
			this._dbfBytes = event.target.data as ByteArray;
			this.doneGettingData();
		}
		
		
		protected function doneGettingData() : void {
			if( this._shpBytes && (this.hasDbf == false || this._dbfBytes)  ){
				var amfs:Array = AmfFactory.CreateFromShapefile( this._shpBytes , this._dbfBytes , true , true );
				var numAmfs:uint = amfs.length;
				if( amfs.length > 0 ){
					var amf:AmfObject = amfs[0] as AmfObject;
					var shapeClass:Class;
					if( amf.geometryType == AmfObject.typePoint ){
						shapeClass = wmgOverlayLayerMarker;
					}
					else if( amf.geometryType == AmfObject.typePolyline ){
						shapeClass = wmgOverlayLayerPolyline;
					}
					else if( amf.geometryType == AmfObject.typePolygon ){
						shapeClass = wmgOverlayLayerPolygon;
					}
					
					for( var i:int = 0; i < numAmfs; i++ ){ 
						this.add( new shapeClass(amfs[i]) as wmgOverlayLayerBaseShape );
					}
				}
				this.dispatchEvent( new Event( "DATA_PULL_COMPLETE" ) );
			}
		}
		
	}
}